package org.itboys.ace.web.lasted;

import org.itboys.admin.entity.lasted.SystemRole;
import org.itboys.admin.service.lasted.SystemRoleService;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *作者：江雄
 * 描述：系统-角色
 * 时间：2017年2月24日.重新构建
 */
@RestController
@RequestMapping("/ace/system/role")
public class AceSystemRoleController extends BaseController{
    @Autowired
    private SystemRoleService roleService;

    @RequestMapping("/role-list")
    public ModelAndView role_list(HttpServletRequest request, HttpServletResponse response, Model model) {
        return new ModelAndView("/ace/sys/role-list");
    }

    /**
     * 分页查询角色
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request, HttpServletResponse response){

            PageResult<SystemRole> result=roleService.selectRole(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);

    }
    /**
     * 保存
     * @param request
     * @param response
     */
    @RequestMapping(value="/findOne", method=RequestMethod.POST)
    public void findOne(@RequestParam(value="id",required = true) Long id, HttpServletRequest request, HttpServletResponse response) {
        try{
          //  roleService.find(role);
            AjaxUtils.renderJson(response,roleService.getById(id));
        }catch(Exception e){
            logger.error("SystemRole findOne error", e);
            e.printStackTrace();
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }
    /**
     * 保存
     * @param request
     * @param response
     */
    @RequestMapping(value="/save", method=RequestMethod.POST)
    public void save(@ModelAttribute SystemRole role, HttpServletRequest request, HttpServletResponse response) {
        try{
            roleService.saveRole(role);
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        }catch(Exception e){
            logger.error("SystemRole save error", e);
            e.printStackTrace();
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }
    /**
     * 删除
     * @param id
     * @param response
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public void delete(@RequestParam(value="id",required = true) Long id, HttpServletResponse response){
        try {
            roleService.delete(id);
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            logger.error("SystemRole delete error", e);
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }



}
